<?php

/*
 * TauxController.php
 */

namespace RHX\Controller;

use RHX\Model\Object\CodeActivite;
use Zend\View\Model\JsonModel;

class TauxController extends RHXController {

    /**
     * Page d'accueil "Gestion des Taux horaires"
     *
     * @return array
     */
    public function indexAction() {
        //tbl
        $tauxProcTbl =  $this->getService('Table\TauxProcessTable');
        
        //$tauxDispo = $tauxProcTbl->getListTauxExists();
	return array(
          
        );
    }
    
     /**
     * Liste des traitements de taux
     *
     * @return array
     */
    public function traitementsAction() {
        //tbl
        
	return array(
            
        );
    }

    public function loadIndexAction() {
	// Changement du Layout
	$this->layout('layout/ajax');
        
        //tables
        $tauxProcessTbl =  $this->getService('Table\TauxProcessTable');
        $tauxProc           =  $tauxProcessTbl->getApplied(false, 'id asc');
        $tauxProcApplied    =  $tauxProcessTbl->getApplied(true, 'id desc');
        
	// Paramèters
	return array(   
            'manager' =>  new \RHX\Model\Manager\TauxManager($this->getAdapter()),
            'tauxProc' => $tauxProc,
            'tauxProcApplied' => $tauxProcApplied            
	);
    }
    
    /*  Charge par ajax la vue des liste des traitemens
     */
    public function loadTaitementAction() {
	// Changement du Layout
	$this->layout('layout/ajax');
        
        //tables
        $tauxProcessTbl =  $this->getService('Table\TauxProcessTable');
        $tauxProc           =  $tauxProcessTbl->getApplied(false, 'id asc');
        $tauxProcApplied    =  $tauxProcessTbl->getApplied(true, 'id desc');
        
	// Paramèters
	return array(   
            'manager' =>  new \RHX\Model\Manager\TauxManager($this->getAdapter()),
            'tauxProc' => $tauxProc,
            'tauxProcApplied' => $tauxProcApplied            
	);
    }
    
    public function loadAction() {
	// Changement du Layout
	$this->layout('layout/ajax');
	
        //data post
        $post = $this->getRequest()->getPost();
	
        //manager
	$tauxManager = new \RHX\Model\Manager\TauxManager($this->getAdapter());                
	$tabTaux = $tauxManager->getTauxPerPoste(); 
        
        // Paramèters
	return array(
	    'tabTaux' => $tabTaux,
	    'manager' => $tauxManager,
            'onglet'  => $post->onglet != '' ? $post->onglet : false
	);
    }
    
    /* retourne la liste des taux horaires actuelle
     * 
     *
     */
    
    public function jsonTauxListAction() {
        //$adapter = $this->getAdapter();
        $tauxProcTable = $this->getService('Table\TauxProcessTable');
        
        $taux = $tauxProcTable->getListTauxExists();
        
        $data = array();
        foreach ($taux as $t ) :
            $data [] = $t['taux'];
        endforeach;
        
        return new JsonModel($data);
    }
    
     /* retourne le nombre des taux horaires actuelle
     * 
     */
    public function jsonTauxListCountAction() {
        $tauxProcTable = $this->getService('Table\TauxProcessTable');        
        $taux = $tauxProcTable->getListTauxExists();
        $data ['count'] = count($taux);
        
        return new JsonModel($data);
    }
    
    /*
     * Ajout d'un taux process
     *
     */    
    public function addTauxAction() {
        $this->layout('layout/ajax');
        $post = $this->getRequest()->getPost();
        
        //table
        $tauxProcTable = $this->getService('Table\TauxProcessTable');
        
        $tauxProc = new \RHX\Model\Object\TauxProcess($tauxProcTable->getAdapter(), $post);
        
        $idNewProc = $tauxProcTable->save($tauxProc);
                
        return new JsonModel(array(
            'success'=> true,
            'id_new_proc' => $idNewProc
            ));
    }
    
    
    /*
     * suppresion d'un taux process via ajax
     *
     */    
    public function eraseAction() {
        $this->layout('layout/ajax');
        $post = $this->getRequest()->getPost();
        
        //table
        $tauxProcTable = $this->getService('Table\TauxProcessTable');
        
        $tauxProc = $tauxProcTable->get($post->pk); 
        
        $tauxProcTable->erase($tauxProc->id);
        
        return array('tauxProc'=> $tauxProc);
        
        //return new JsonModel(array('success'=> true));
    }
    
    /*
     * 
     * */
    public function saveNumberDataAction(){
        $this->layout('layout/ajax');
        
        // data post & param
        $post = $this->getRequest()->getPost();
        $pk = $post->get('pk');
        $name = $post->get('name');
        $value = $post->get('value');
        
        //table
        $tauxProcTable = $this->getService('Table\TauxProcessTable');
        
        $value = addslashes($value);

        $sql = "UPDATE rh_taux_process SET $name = $value WHERE id = $pk";
        $result = $tauxProcTable->executeQuery($sql);
        
        return array(
            'sql' => $sql,
            'result' => $result,
        );
        
    }
    
    public function saveStringDataAction(){
        $this->layout('layout/ajax');
        
        // data post & param
        $post = $this->getRequest()->getPost();
        $pk = $post->get('pk');
        $name = $post->get('name');
        $value = $post->get('value');
        
        //table
        $tauxProcTable = $this->getService('Table\TauxProcessTable');
        
        $value = addslashes($value);

        $sql = "UPDATE rh_taux_process SET $name = '$value' WHERE id = $pk";
        
        $result = $tauxProcTable->executeQuery($sql);
        
        return array(
            'sql' => $sql,
            'result' => $result,
        );
        
    }
    
    /*
     * action qui va appliquer des changement des  taux horaire de tous les employé concerné de tous les site
     * 
     */
    public function applyAction(){
	$this->layout('layout/ajax');
	
	//table
        $tauxProcTbl = $this->getService('Table\TauxProcessTable');
	
	//data 
	$post = $this->getRequest()->getPost();
	//$idTauxProc =  1 ; // 12.75 ->>...
	$idTauxProc = $post->pk;
	
	$tauxProc = $tauxProcTbl->get( $idTauxProc );	
	$tauxManager = new \RHX\Model\Manager\TauxManager($this->getAdapter());
	
	// retrieving list employe having taux $tauxProc->taux
	var_dump($tauxProc);
	print("taux horaire concerné ". $tauxProc->taux);
	
	
	$successManag = $tauxManager->updateEmployesHavingTaux($tauxProc->taux, $tauxProc->nouveau_taux, $tauxProc->date_application);
	
	if( $successManag ){
	    $tauxProc->is_applied = true;
	    $tauxProcTbl->save( $tauxProc );	
	}
	
	$return = array();	
	return $return;
    }
}

?>
